package com.drama.api.sys.dramas.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.drama.api.base.*;
import com.drama.api.sys.download.domain.vo.DownloadExamineAddVO;
import com.drama.api.sys.download.service.IVideoDownloadExamineService;
import com.drama.api.sys.dramas.domain.vo.*;
import com.drama.api.sys.dramas.service.ISysDramasService;
import com.drama.util.BindingResultUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;


@Api( tags = ApiIndex.SysDramasController)
@RequestMapping(value = "/api/sys/dramas")
@RestController
@SaCheckLogin
public class SysDramasController {

    @Autowired
    private ISysDramasService service;
    @Autowired
    private IVideoDownloadExamineService examineService;


    @ApiOperation(value = "查询剧目列表")
    @GetMapping(value = "/list")
    public R<RestPage<List<SysDramasVO>>> list(
            @ApiParam(value = "查询参数") @ModelAttribute SysDramasSearchVO searchVO) {
        List<SysDramasVO> list = service.getList(searchVO);
        RestPage restPage = new RestPage(new PageInfo(list), list);
        return new R(restPage);
    }

    @ApiOperation(value = "查询剧目详情")
    @GetMapping(value = "/one")
    public R<SysDramasVO> export_zip(@ApiParam(value = "剧目Id") @ModelAttribute IDEntity idEntity) {
        return new R( service.getOne(idEntity.getId()));
    }


    @ApiOperation(value = "查询剧目状态数量统计")
    @GetMapping(value = "/count")
    public R<List<SysDramasCountVO>> count(@ApiParam(value = "查询参数") @ModelAttribute SysDramasCountSearchVO searchVO) {
        List<SysDramasCountVO> list = service.count(searchVO);
        return new R(list);
    }

    @ApiOperation(value = "查询剧目视频列表")
    @GetMapping(value = "/video_list")
    public R<RestPage<List<SysVideoVO>>> video_list(
            @ApiParam(value = "查询参数") @ModelAttribute SysVideoSearchVO searchVO) {
        List<SysVideoVO> list = service.getVideoList(searchVO);
        RestPage restPage = new RestPage( new PageInfo(list),list);
        return new R(restPage);
    }

    @ApiOperation(value = "查询剧目进度记录列表")
    @GetMapping(value = "/schedule_list")
    public R<List<SysDramasScheduleVO>> schedule_list(
            @ApiParam(value = "查询参数") @ModelAttribute SysDramasScheduleSearchVO searchVO) {
        List<SysDramasScheduleVO> list = service.getScheduleList(searchVO);
        return new R(list);
    }

    @ApiOperation(value = "剧目提交审核")
    @PostMapping(value = "/refer_examine")
    public R refer_examine(@ApiParam(value = "参数") @Valid @RequestBody SysDramasExamineVO vo, BindingResult bindingResult) {
        R result = BindingResultUtil.BindingResult(bindingResult);
        if (result.getCode() == Constants.SYS_SUCCESS) {
            service.referExamine(vo);
        }
        return new R();
    }

    @ApiOperation(value = "更新剧目备案号")
    @PostMapping(value = "/update_drama_no")
    public R update_drama_no(@ApiParam(value = "参数") @Valid @RequestBody SysDramasNoVO vo, BindingResult bindingResult) {
        R result = BindingResultUtil.BindingResult(bindingResult);
        if (result.getCode() == Constants.SYS_SUCCESS) {
            service.updateDramaNo(vo);
        }
        return new R();
    }



    @ApiOperation(value = "视频下载提交审核")
    @PostMapping(value = "/video_add_examine")
    public R video_add_examine(@ApiParam(value = "参数") @Valid @RequestBody DownloadExamineAddVO vo, BindingResult bindingResult) {
        R result = BindingResultUtil.BindingResult(bindingResult);
        if (result.getCode() == Constants.SYS_SUCCESS) {
            examineService.addExamine(vo);
        }
        return new R();
    }



    @ApiOperation(value = "导出剧目信息ZIP")
    @GetMapping(value = "/export_zip")
    public void export_zip(@ApiParam(value = "剧目Id") @ModelAttribute IDEntity idEntity, HttpServletResponse response, HttpServletRequest request) {
        service.export_zip(idEntity,response,request);
    }



    @ApiOperation(value = "剧目完成转码")
    @PostMapping(value = "/complete_transcoding")
    public R complete_transcoding(@ApiParam(value = "剧目Id") @Valid @RequestBody IDEntity idEntity) {
        service.completeTranscoding(idEntity.getId());
        return new R();
    }

    @ApiOperation(value = "剧目上线审核")
    @PostMapping(value = "/go_live_examine")
    public R go_live_examine(@ApiParam(value = "参数") @Valid @RequestBody SysDramasGoLineExamineVO vo, BindingResult bindingResult) {
        R result = BindingResultUtil.BindingResult(bindingResult);
        if (result.getCode() == Constants.SYS_SUCCESS) {
            service.goLiveExamine(vo);
        }
        return new R();
    }


    @ApiOperation(value = "查询历史审核列表")
    @GetMapping(value = "/examine_list")
    public R<RestPage<List<SysDramasExamineLogVO>>> examine_list(
            @ApiParam(value = "查询参数") @ModelAttribute SysDramasExamineLogSearchVO searchVO) {
        List<SysDramasExamineLogVO> list = service.getExamineList(searchVO);
        RestPage restPage = new RestPage(new PageInfo(list), list);
        return new R(restPage);
    }

}